package com.stockz.equity.util;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceUnit;
import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.jpa.EntityManagerFactoryInfo;
import org.springframework.stereotype.Service;

@Service
public class JpaUtil {

	@PersistenceUnit(unitName="Stockz")
    @Autowired
    private EntityManagerFactory entityManagerFactory;
    
    public EntityManager getEntityManager() {
        return entityManagerFactory.createEntityManager();
    }
    
    public DataSource getDataSource(){
        if (entityManagerFactory instanceof EntityManagerFactoryInfo) {
            EntityManagerFactoryInfo emfInfo = (EntityManagerFactoryInfo)entityManagerFactory;
            return emfInfo.getDataSource();
        }
        return null;
    }
}